package com.event;

import com.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;

/**
 * @author 大忽悠
 * @create 2022/9/15 20:54
 */
@Slf4j
public class DomainEventPublisher implements ApplicationContextAware {
    private ApplicationEventPublisher eventPublisher;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
           eventPublisher=applicationContext;
    }

    public void publishEvent(DomainEvent domainEvent){
        log.info("当前时间: {} ,发布的领域事件为: {}", TimeUtil.getCurTime(),domainEvent);
        eventPublisher.publishEvent(domainEvent);
    }
}
